Page({
  data: {
    passport: {}
  },
  onLoad ({ id, encryptedData }) {
    this.getPassport(id)
    console.log(encryptedData)
    this.getPassportShare(encryptedData)
  },
  async getPassport (id) {
    if (!id) return
    const { code, data } = await wx.http.get('/visitor/' + id)
    if (!code === 10000) wx.utils.toast()
    console.log(data)
    this.setData({ passport: data })
  },
  async getPassportShare (encryptedData) {
    if (!encryptedData) return
    const { code, data: passport } = await wx.http.get('/visitor/share/' + encryptedData)
    // 检测接口是否调用成功
    if (code !== 10000) return wx.utils.toast()
    // 渲染数据
    this.setData({ passport })
  },
  onShareAppMessage () {
    const { encryptedData } = this.data.passport
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index?encryptedData=' + encryptedData,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png'
    }
  },
  async saveQRCode () {
    try {
      // 读取图片信息
      const { path } = await wx.getImageInfo({
        // 二维码的图片路径
        src: this.data.passport.url
      })
      // 保存图片到相册
      wx.saveImageToPhotosAlbum({ filePath: path })
    } catch (err) {
      wx.utils.toast('保存图片失败，稍后重试!')
    }
  }
})
